function Dropdown(node, value) {
    this.init(node)
}
Dropdown.prototype = {
    init: function (node, value) {
        this.node = node;
        this.list = [];
        this.value = $(node).find('input').val() || '';
        this.createTemplate();
        this.open();
        this.selectList();
        this.cardEvent();
        this.input(this.list);
    },
    open: function () {
        var _this = this
        $(this.node).on('click', 'input', function (evnet) {
            evnet.stopPropagation();
            $(document).on('click', _this.documentEvent);
            $('.dropDown-card').show();
            $('.dropDown .dropDown-select i').addClass('dropDown-icon-active');
        });
    },
    LiNode: function (arr) {
        var Li = ''
        for (var i = 0; i < arr.length; i++) {
            Li += `<li class="dropDown-item p-12 ${this.value == arr[i].value ? 'dropDown-active' : ''}">${arr[i].value}</li>`
        }
        if (arr.length == 0) {
            Li = `<div class="null">
                <img alt="暂无列表"/>
                <div>暂无列表</div>
              </div>`
        }
        return Li
    },
    //创建下拉数据
    createTemplate: function () {
        var Li = $('.dropDown-card li'),
            dataList = []
        for (var i = 0; i < Li.length; i++) {
            var value = Li.eq(i).text(),
                id = Li.eq(i).attr('data-id')
            dataList.push({
                id: id,
                value: value
            })
        }
        this.list = dataList
       
    },
    //文档事件
    documentEvent: function (evnet) {
        evnet.stopPropagation();
        $('.dropDown-card').hide();
        $('.dropDown .dropDown-select i').removeClass('dropDown-icon-active');
        setTimeout(function () {
            $(document).off('click')
        }, 500)
    },
    //阻止下拉板块冒泡事件
    cardEvent: function () {
        $(this.node).on('click', '.dropDown-card', function (evnet) {
            evnet.stopPropagation();
        });
    },
    //点击选中列表数据
    selectList: function () {
        var _this = this
        $(this.node).on('click', '.dropDown-item', function (evnet) {
            evnet.stopPropagation();
            var value = $(this).text(),
                id = $(this).attr('data-id');
            $(this).addClass('dropDown-item-active').siblings().removeClass('dropDown-item-active');
            $(_this.node).find('input').val(value);
            $(_this.node).find('.hidden-input').val(id);
            $('.dropDown-card').hide();
            $('.dropDown .dropDown-select i').removeClass('dropDown-icon-active');
        })
    },
    //设置列表数据
    setList: function (list) {
        $('.dropDown-card').html('')
        $('.dropDown-card').append(this.LiNode(list))
    },
    //输入框搜索事件
    input: function (arr) {
        var list = this.deepClone(arr),
            _this = this
        $(this.node).find('input').on('input', function (e) {
            var key = $(this).val(),
                result = _this.getArrayByName(key, list);
            if (key != '') {
                _this.setList(result)
            } else {
                _this.setList(list)
            }

        })
    },
    //搜索
    getArrayByName: function (value, array) {
            var result = []
            for (var i = 0; i < array.length - 1; i++) {
                var isStr = array[i].value.indexOf(value) != -1
                if (isStr) {
                    result.push(array[i]);
                }
            }
            array = result
        return array
    },
    //对象深度克隆
    deepClone: function (obj) {
        var newObj = obj instanceof Array ? [] : {};
        for (var i in obj) {
            newObj[i] = typeof obj[i] == 'object' ?
                this.deepClone(obj[i]) : obj[i];
        }
        return newObj;
    }

}